{
"ael_seal": "AEL CS Encyclopedia β Β© Ayman Elmasry",
"owner": "Ayman Elmasry",
"legal_entities": [
"Ayman Elmasry LLC (UAE)",
"Ayman Elmasry Advertising & Marketing (Egypt)"
],
"section": "02_Computer_Science_Notes (Week 1 C)",
"syllabus_source": "Harvard CS50x (Deep Internal Analysis)",
"methodology": "8-Stage Sub-Silicon Execution Paradigm",
"system_version": "v3.0"
}
C is a procedural, general-purpose language that affords direct memory access and low-level architectural control. In this pillar, we delve into the rigorous academic foundations of computer science, referencing the invaluable code archive src1 and lecture1.pdf.
===================================================================================
THE ANATOMY OF A C PROGRAM (hello2.c)
===================================================================================
#include <cs50.h> ββ> Preprocessor Directive (CS50 Library)
#include <stdio.h> ββ> Preprocessor Directive (Standard Input/Output)
int main(void) ββ> Main Entry Point (Returns int 0 on success, takes void)
{
string answer = get_string("What's your name? "); ββ> Variable Instantiation
printf("hello, %s\n", answer); ββ> Formatted Output (%s)
}
===================================================================================
C is a statically typed language. Every variable must be declared with an explicit data type, defining both its exact memory footprint in bytes and the semantic interpretation of its underlying binary sequences.
int: Integers. Typically occupies 4 bytes (32 bits) of memory, representing values from -2,147,483,648 to 2,147,483,647. (Directly referenced in calculator0.c).char: Single character. Occupies 1 byte (8 bits) and is mapped to numeric integer equivalents via the ASCII standard table. (Referenced in agree0.c).float: Single-precision floating-point numbers. Occupies 4 bytes. Susceptible to floating-point imprecision due to limited fractional bits. (Referenced in calculator2.c).double: Double-precision floating-point numbers. Occupies 8 bytes (64 bits), affording expanded mantissa precision for rigorous computational calculations.bool: Boolean values (true / false). Accessible via the <cs50.h> library or standard <stdbool.h>.string: Character strings. Structurally represented as an array of characters (Array of chars) terminated by a null byte (\0).Control flow constructs empower programs to make dynamic runtime decisions based on evaluating boolean conditions. Here we examine paradigms extracted from compare and agree codebases.
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("x is equal to y\n");
}
&& (AND): Evaluates to true if and only if both operands are true.|| (OR): Evaluates to true if at least one operand is true.! (NOT): Unary negation operator; inverts the boolean state of its operand.Repetition is the bedrock of computational efficiency. Rather than duplicating logic, we leverage loop constructs and function abstraction, as demonstrated in cat and mario.
while: Condition-controlled loops. Optimal when the exact iteration count is unknown prior to runtime.for: Counter-controlled loops featuring explicit initialization, condition evaluation, and update clauses (Initialization; Condition; Update).do-while: Post-test loops. Guaranteed to execute the inner block at least once prior to evaluating the exit condition. Representing the gold standard for robust user input validation (Input Validation).
// Input Validation using do-while (mario5.c)
int n;
do
{
n = get_int("Size: ");
}
while (n < 1);
In cat15.c, Malan elucidates a foundational rule of C compilers: source files are parsed sequentially from top to bottom. Calling a custom function like meow(3) before it is explicitly declared results in a fatal compiler error.
Architectural Solution: Declaring the function signature at the top of the file (void meow(int n);), while housing the actual implementation logic below the main entry point.
#include <stdio.h>
void meow(int n); // Prototype Declaration
int main(void)
{
meow(3);
}
void meow(int n) // Internal Implementation
{
for (int i = 0; i < n; i++)
{
printf("meow\n");
}
}